﻿Imports System.IO
Imports System.Drawing.Imaging
Public Class MainUI
    Private Const ApplicationTitle As String = "Capture It"
    Private Const WelcomeInfo As String = "Welcome to Capture It"
    Private Const uxCapture_Text As String = "Capture"
    Private Const ConfirmQuit As String = "Captured screenshot not saved. Would you like to save it?"
    Private Const InfoMessage As String = "Capture it will help you to capture screenshots.{0}Current capture mode -{1}"
    Private Const SaveDialog_Title As String = "Save screenshot - Capture It"
    Private Const CopySuccessMessage As String = "Captured screen shot copied to clipborad."
    Private _IsSaved As Boolean = False
    Private _Filename As String = String.Empty
    Private _TempFilename As String = String.Empty
    Private _CurrentCaptureMode As ConfigSettings.CaptureType = ConfigSettings.CaptureType.FullScreen
    Private Sub LocalizeUI()
        Me.Text = ApplicationTitle
        Me.uxStatusLabel.Text = WelcomeInfo
        Me.uxCapture.Text = uxCapture_Text
    End Sub
    Private Sub LoadSettings()
        Dim currentSettings As Settings = ConfigSettings.ReadSettings
        Me._CurrentCaptureMode = currentSettings.OptionType
    End Sub
    Private Sub MainUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FileManagement.CheckEnviorment()
        Me.LoadSettings()
        Me.uxInfo.Text = String.Format(InfoMessage, Environment.NewLine, Me._CurrentCaptureMode.ToString)
        Me.LocalizeUI()
    End Sub
    Private Sub uxCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCapture.Click
        Dim QuitConfirmation As DialogResult
        If Not Me._IsSaved AndAlso Me._TempFilename.Length >= 1 Then
            QuitConfirmation = MessageBox.Show(ConfirmQuit, ApplicationTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

            Select Case QuitConfirmation
                Case Windows.Forms.DialogResult.Yes
                    If Me.SaveFileAs() Then
                        Me.InitNewCapture()
                    End If
                Case Windows.Forms.DialogResult.No
                    Me.InitNewCapture()
                Case Windows.Forms.DialogResult.Cancel
                    'do nothing
            End Select
        Else
            Me.InitNewCapture()
        End If
    End Sub
    Private Sub InitNewCapture()
        Me.LoadSettings()
        Select Case Me._CurrentCaptureMode
            Case ConfigSettings.CaptureType.FullScreen
                Me.CaptureFullScreen()
            Case ConfigSettings.CaptureType.Rectangle
                Me.CaptureRectangle()
            Case ConfigSettings.CaptureType.SelectedWindow
                Me.CaptureSelectedWindow()
        End Select
    End Sub
    Private Sub NewCapture()
        Dim QuitConfirmation As DialogResult = Me.Confirm
        Select Case QuitConfirmation
            Case Windows.Forms.DialogResult.Yes
                If Me.SaveFileAs() Then
                    Me.ResetApp()
                End If
            Case Windows.Forms.DialogResult.No
                Me.ResetApp()
        End Select
    End Sub
    Private Sub CaptureSelectedWindow()
        Dim overlay As New OverlayUI(Me._CurrentCaptureMode)
        AddHandler overlay.ScreenCaptured, AddressOf overlay_ScreenCaptured
        Me.WindowState = FormWindowState.Minimized
        overlay.ShowDialog(Me)
    End Sub
    Private Sub CaptureFullScreen()
        Me.WindowState = FormWindowState.Minimized
    End Sub
    Private Sub CaptureRectangle()
        Dim overlay As New OverlayUI(Me._CurrentCaptureMode)
        AddHandler overlay.ScreenCaptured, AddressOf overlay_ScreenCaptured
        Me.WindowState = FormWindowState.Minimized
        overlay.WindowState = FormWindowState.Maximized
        overlay.ShowDialog(Me)
    End Sub
    Public Sub overlay_ScreenCaptured(ByVal e As ImageCapturedEventArguments)
        Me._TempFilename = e.Filename
        Me.WindowState = FormWindowState.Normal
    End Sub
    Private Sub uxExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxExit.Click
        Me.Close()
    End Sub

    Private Sub uxSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSave.Click
        If Me._Filename.Length >= 1 AndAlso Me._TempFilename.Length >= 1 Then
            Me.SaveFile()
        Else
            Me.SaveFileAs()
        End If
    End Sub

    Private Sub uxSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSaveAs.Click
        If Me._TempFilename.Length >= 1 Then
            Me.SaveFileAs()
        End If
    End Sub
    Private Function SaveFileAs() As Boolean
        Dim result As Boolean = False
        Using saveDialog As New SaveFileDialog
            With saveDialog
                .AddExtension = True
                .DefaultExt = "BMP"
                .FileName = Me._Filename
                .Filter = "Bmp Files(*.bmp)|*.bmp|Jpeg Files(*.jpg)|*.jpg|Png Files(*.png)|*.png|Gif Files(*.gif)|*.gif"
                .FilterIndex = 3
                Select Case Path.GetExtension(Me._Filename).ToUpperInvariant
                    Case ".BMP"
                        .FilterIndex = 1
                    Case ".JPG"
                        .FilterIndex = 2
                    Case ".PNG"
                        .FilterIndex = 3
                    Case ".GIF"
                        .FilterIndex = 4
                End Select
                .OverwritePrompt = True
                .AutoUpgradeEnabled = False
                .CheckPathExists = True
                .DereferenceLinks = True
                .SupportMultiDottedExtensions = True
                .ValidateNames = True
                .Title = SaveDialog_Title
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Me._Filename = .FileName
                    Me.SaveFile()
                    result = True
                End If
            End With
        End Using
        Return result
    End Function
    Private Sub SaveFile()
        Dim currentImageFormat As ImageFormat = ImageFormat.Bmp
        Select Case Path.GetExtension(Me._Filename).ToUpperInvariant
            Case ".BMP"
                currentImageFormat = ImageFormat.Bmp
            Case ".JPG"
                currentImageFormat = ImageFormat.Jpeg
            Case ".PNG"
                currentImageFormat = ImageFormat.Png
            Case ".GIF"
                currentImageFormat = ImageFormat.Gif
        End Select
        Using Bmp As New Bitmap(Me._TempFilename)
            Bmp.Save(Me._Filename, currentImageFormat)
        End Using
        Me._IsSaved = True
    End Sub
    Private Function Confirm() As DialogResult
        Dim QuitConfirmation As DialogResult
        If Not Me._IsSaved AndAlso Me._TempFilename.Length >= 1 Then
            QuitConfirmation = MessageBox.Show(ConfirmQuit, ApplicationTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
        End If
        Return QuitConfirmation
    End Function
    Private Sub MainUI_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim QuitConfirmation As DialogResult = Me.Confirm
        Select Case QuitConfirmation
            Case Windows.Forms.DialogResult.Yes
                If Not Me.SaveFileAs() Then
                    e.Cancel = True
                End If
            Case Windows.Forms.DialogResult.No
                e.Cancel = False
            Case Windows.Forms.DialogResult.Cancel
                e.Cancel = True
        End Select
    End Sub

    Private Sub uxAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxAbout.Click
        Dim about As New AboutUI
        about.ShowDialog(Me)
    End Sub

    Private Sub uxVisitHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxVisitHome.Click
        Dim url As String = "http://captureit.codeplex.com"
        Process.Start(url)
    End Sub

    Private Sub uxOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxOptions.Click
        Dim options As New OptionsUI
        options.ShowDialog(Me)
    End Sub

    Private Sub uxNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxNew.Click
        Dim QuitConfirmation As DialogResult = Me.Confirm
        Select Case QuitConfirmation
            Case Windows.Forms.DialogResult.Yes
                If Me.SaveFileAs() Then
                    Me.ResetApp()
                End If
            Case Windows.Forms.DialogResult.No
                Me.ResetApp()
        End Select
    End Sub
    Private Sub ResetApp()
        Me._Filename = String.Empty
        Me._IsSaved = False
        Me._TempFilename = String.Empty
        FileManagement.CheckEnviorment()
    End Sub

    Private Sub uxFile_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxFile.DropDownOpening
        Me.uxSave.Enabled = Me._TempFilename.Length >= 1
        Me.uxSaveAs.Enabled = Me._TempFilename.Length >= 1
        Me.uxCopy.Enabled = Me._TempFilename.Length >= 1
    End Sub

    Private Sub MainUI_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me._CurrentCaptureMode = ConfigSettings.CaptureType.FullScreen AndAlso _
            Me.WindowState = FormWindowState.Minimized Then
            Me._TempFilename = FileManagement.GetTempFileName
            Dim region As New Rectangle(0, 0, SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height)
            Using bmp As New Bitmap(region.Width, region.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                Using bitmapGraphics As Graphics = Graphics.FromImage(bmp)
                    bitmapGraphics.CopyFromScreen(region.Left, region.Top, 0, 0, region.Size)
                    bmp.Save(Me._TempFilename)
                End Using
            End Using
            overlay_ScreenCaptured(New ImageCapturedEventArguments(Me._TempFilename))
        End If
    End Sub

    Private Sub uxCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCopy.Click
        If Me._TempFilename.Length >= 1 Then
            Clipboard.SetImage(Image.FromFile(Me._TempFilename))
            MessageBox.Show(CopySuccessMessage, ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class
